package pers.mobian.springsixth.connection;

import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Date 2021/7/17 16:49
 * @Created mobian
 * @Description 用于生成mapper的代理对象
 */
public class MobianFactoryBean implements FactoryBean {

	// mapper文件属性，用于完成属性的初始化
	private Class mapper;

	public MobianFactoryBean(Class mapper) {
		this.mapper = mapper;
	}

	@Override
	public Object getObject() throws Exception {

		Object proxyObject = Proxy.newProxyInstance(MobianFactoryBean.class.getClassLoader(), new Class[]{mapper}, new InvocationHandler() {
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

				System.out.println("执行了对应的代理逻辑...");
				System.out.println(method);
				System.out.println(method.getName());

				return null;
			}
		});

		return proxyObject;
	}

	@Override
	public Class<?> getObjectType() {
		return mapper;
	}
}
